import { CoolController, BaseController } from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { PayBalanceEntity } from '../../entity/balance';
import { PayBalanceService } from '../../service/balance';
import { UserInfoEntity } from '../../../user/entity/info';

/**
 * 支付模块-管理端余额控制器
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: PayBalanceEntity,
  service: PayBalanceService,
  pageQueryOp: {
    fieldEq: ['a.userId', 'a.status'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'user',
        condition: 'user.id = a.userId',
        type: 'leftJoin',
      },
    ],
    select: [
      'a.*',
      'user.nickName as userNickName',
      'user.phone as userPhone',
    ],
  },
})
export class AdminPayBalanceController extends BaseController {
  @Inject()
  payBalanceService: PayBalanceService;

  /**
   * 系统调整用户余额（增加）
   */
  @Post('/adjust/add')
  async adjustBalanceAdd(@Body() body: { 
    userId: number; 
    amount: number; 
    description: string;
    operatorId: number;
  }) {
    const { userId, amount, description, operatorId } = body;

    if (!userId) {
      return this.fail('用户ID不能为空');
    }

    if (!amount || amount <= 0) {
      return this.fail('调整金额必须大于0');
    }

    if (!description) {
      return this.fail('调整说明不能为空');
    }

    const result = await this.payBalanceService.adjustBalance(
      userId,
      amount,
      'add',
      description,
      operatorId
    );

    return this.ok(result);
  }

  /**
   * 系统调整用户余额（扣减）
   */
  @Post('/adjust/deduct')
  async adjustBalanceDeduct(@Body() body: { 
    userId: number; 
    amount: number; 
    description: string;
    operatorId: number;
  }) {
    const { userId, amount, description, operatorId } = body;

    if (!userId) {
      return this.fail('用户ID不能为空');
    }

    if (!amount || amount <= 0) {
      return this.fail('调整金额必须大于0');
    }

    if (!description) {
      return this.fail('调整说明不能为空');
    }

    const result = await this.payBalanceService.adjustBalance(
      userId,
      amount,
      'deduct',
      description,
      operatorId
    );

    return this.ok(result);
  }

  /**
   * 冻结用户余额
   */
  @Post('/freeze')
  async freezeBalance(@Body() body: { 
    userId: number; 
    amount: number; 
    description: string;
    operatorId: number;
  }) {
    const { userId, amount, description, operatorId } = body;

    if (!userId) {
      return this.fail('用户ID不能为空');
    }

    if (!amount || amount <= 0) {
      return this.fail('冻结金额必须大于0');
    }

    if (!description) {
      return this.fail('冻结说明不能为空');
    }

    const result = await this.payBalanceService.freezeBalance(
      userId,
      amount,
      null,
      null,
      description,
      operatorId,
      2 // 管理员操作
    );

    return this.ok(result);
  }

  /**
   * 解冻用户余额
   */
  @Post('/unfreeze')
  async unfreezeBalance(@Body() body: { 
    userId: number; 
    amount: number; 
    description: string;
    operatorId: number;
  }) {
    const { userId, amount, description, operatorId } = body;

    if (!userId) {
      return this.fail('用户ID不能为空');
    }

    if (!amount || amount <= 0) {
      return this.fail('解冻金额必须大于0');
    }

    if (!description) {
      return this.fail('解冻说明不能为空');
    }

    const result = await this.payBalanceService.unfreezeBalance(
      userId,
      amount,
      null,
      null,
      description,
      operatorId,
      2 // 管理员操作
    );

    return this.ok(result);
  }
}